﻿using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shell;

namespace Compiled.Win8StartWPF
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : Window
	{
		public MainWindow()
		{
			this.Icon = Properties.Resources.StartMenu.ToImageSource();
			InitializeComponent();

			RefreshUserName();
			ResetSize();
			ResetBackground();
			InitSystemMenu();

			PinnedApps.Folder = @"C:\Users\bqcw37\AppData\Roaming\Microsoft\Windows\Start Menu\Programs";
		}

		private void InitSystemMenu()
		{
			
		}

		private void Window_Loaded(object sender, RoutedEventArgs e)
		{
		}

		void ResetBackground()
		{
			Color c = SystemColors.WindowFrameBrush.Color;
			c.A = 128;
			MainDock.Background = new SolidColorBrush(c);
		}

		public void ShowMenu()
		{
			ResetSize();
			this.WindowState = System.Windows.WindowState.Normal;
		}

		public void HideMenu()
		{
			this.WindowState = System.Windows.WindowState.Minimized;
		}

		void ResetSize()
		{
			this.Left = 0;
			this.Height = System.Windows.SystemParameters.FullPrimaryScreenHeight + System.Windows.SystemParameters.CaptionHeight;
			MainGrid.Height = this.Height * Properties.Settings.Default.MenuHeight;
		}

		private void RefreshUserName()
		{
			string username = Environment.GetEnvironmentVariable("USERNAME");
			if (username == "" || username == null)
				UsernameBox.Visibility = System.Windows.Visibility.Collapsed;
			else
			{
				UsernameBox.Visibility = System.Windows.Visibility.Visible;
				UsernameBox.Text = username;
			}

			string userfile;
			userfile = Environment.ExpandEnvironmentVariables("%TEMP%\\%USERDOMAIN%+%USERNAME%.BMP");
			if (!System.IO.File.Exists(userfile))
				userfile = Environment.ExpandEnvironmentVariables("%TEMP%\\%USERNAME%.BMP");
			if (!System.IO.File.Exists(userfile))
			{
				UserIcon.Visibility = System.Windows.Visibility.Hidden;
			}
			else
			{
				BitmapImage icon = new BitmapImage();
				icon.BeginInit();
				icon.UriSource = new Uri(userfile);
				icon.EndInit();
				UserIcon.Source = icon;
			}
		}

		private void PinnedApps_ItemClicked(ShortcutPanel sender, IconButton clickedItem)
		{
			HideMenu();
		}

		private void Window_Deactivated_1(object sender, EventArgs e)
		{
			HideMenu();
		}


	}
}
